/**
 * Copyright (C) 2014-2019 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>
 * Any modifications to this file must keep this entire header intact.
 */

package com.symaster.common.pixy.image.png;

import com.symaster.common.util.Builder;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

/**
 * PNG IDAT chunk builder
 *
 * @author Wen Yu, yuwen_66@yahoo.com
 * @version 1.0 04/26/2013
 */
public class IDATBuilder extends ChunkBuilder implements Builder<Chunk> {

    private final ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
    private Deflater deflater = new Deflater(5);
    private boolean finish;

    public IDATBuilder() {
        super(ChunkType.IDAT);
    }

    public IDATBuilder(int compressionLevel) {
        this();
        deflater = new Deflater(compressionLevel);
    }

    public IDATBuilder data(byte[] data, int offset, int length) {
        // Caches the bytes
        bout.write(data, offset, length);

        return this;
    }

    public IDATBuilder data(byte[] data) {
        return data(data, 0, data.length);
    }

    @Override
    protected byte[] buildData() {
        // Compresses raw data
        deflater.setInput(bout.toByteArray());

        bout.reset();
        byte[] buffer = new byte[4096];

        if (finish)
            // This is to make sure we get all the input data compressed
            deflater.finish();

        while (!deflater.finished()) {
            int bytesCompressed = deflater.deflate(buffer);
            if (bytesCompressed <= 0) break;
            bout.write(buffer, 0, bytesCompressed);
        }

        byte[] temp = bout.toByteArray();

        bout.reset();

        return temp;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }
}
